/* File 2 of 2 for Appendix Table 6 */


#delimit ;
clear all;

local outfile "GWgapr10_pfpool_TLsur_LgXten";
set more off;


di _n "$S_DATE $S_TIME";









********************************************************************************;
* Setting up locals for regs;

local labels `"
	phi_f:_cons "Phi female"
	phi_lt25:_cons "Phi age less than 25 yrs"
	phi_40to54:_cons "Phi sge 40 to 54"
	phi_55p:_cons "Phi age 55 plus"
	phi_pt:_cons "Phi part time (<1 FTE)"
	delta:_cons "Delta (exp on av FTEs or hours)"
	
	lnL:_cons "Labour input (ln)"
	xb_multiplant:_cons "Multiplant"
	xb_WPc_WP_cat_1:_cons "1 working proprietor"
	xb_WPc_WP_cat_2:_cons "2-5 working proprietors"
	xb_WPc_WP_cat_3:_cons "6-10 working proprietors"
	xb_WPc_WP_cat_4:_cons ">10 working proprietors"
	
	xb_s_fthc_25to39:_cons "Share of full time employees aged 25 to 39"
	xb_s_pthc_25to39:_cons "Share of part time employees aged 25 to 39"
	xb_s_fthc_40to54:_cons "Share of full time employees aged 40 to 54"
	xb_s_pthc_40to54:_cons "Share of part time employees aged 40 to 54"
	xb_s_fthc_55p:_cons "Share of full time employees aged 55+"
	xb_s_pthc_55p:_cons "Share of full time employees aged 55+"
	
	xb_s_fthc_ten1:_cons "Share of full time employees in 2nd year at pent"
	xb_s_pthc_ten1:_cons "Share of part time employees in 2nd year at pent"
	xb_s_fthc_ten2:_cons "Share of full time employees in 3rd+ year at pent"
	xb_s_pthc_ten2:_cons "Share of part time employees in 3rd+ year at pent"
	
	xb_sec_govt_prod_ent:_cons "Sector: Government producer enterprise"
	xb_sec_fin_inter:_cons "Sector: Financial intermediary"
	xb_avg_age:_cons "Average employee age (/100)"
	xb_female:_cons "Female"
	"';

	
local order "phi_f_ten0_pf:_cons phi_f_ten1_pf:_cons phi_f_ten2_pf:_cons 
	phi_ten1_pf:_cons phi_ten2_pf:_cons 
	phi_pt_pf:_cons delta_pf:_cons
	phi_lt25_pf:_cons phi_40to54_pf:_cons phi_55p_pf:_cons
	
	phi_f_ten0_wb:_cons phi_f_ten1_wb:_cons phi_f_ten2_wb:_cons 
	phi_ten1_wb:_cons phi_ten2_wb:_cons 
	phi_pt_wb:_cons delta_wb:_cons
	phi_lt25_wb:_cons phi_40to54_wb:_cons phi_55p_wb:_cons
	";
	

* uncomment this line to make the output tidier;
*local indicators `"
	"Year fixed effects (pf) = xb_pf_Yyear_*:_cons"
	"Year fixed effects (wb) = xb_wb_Yyear_*:_cons"
	"Productivity industry fixed effects (pf) = xb_pf_INind*:_cons"
	"Productivity industry fixed effects (wb) = xb_wb_INind*:_cons"	
	"'; 
	
	


	
	


******************************;
* Specifying the labour input;	

* head count*(average fte)^delta;

local L_fte_del_pf "((
					  (L_hc_m_ten0 *(av_fte_m_ten0)^{delta_pf=1} 
	+ {phi_f_ten0_pf=1}*L_hc_f_ten0 *(av_fte_f_ten0)^{delta_pf}) 
	+ {phi_ten1_pf=1}    *(L_hc_m_ten1*(av_fte_m_ten1)^{delta_pf} 
	+ {phi_f_ten1_pf=1}*L_hc_f_ten1 *(av_fte_f_ten1)^{delta_pf}) 
	+ {phi_ten2_pf=1}  *(L_hc_m_ten2*(av_fte_m_ten2)^{delta_pf} 
	+ {phi_f_ten2_pf=0.9}*L_hc_f_ten2 *(av_fte_f_ten2)^{delta_pf}))*
	 ({phi_lt25_pf=0.5}*L_hc_lt25*(av_fte_lt25)^{delta_pf} + L_hc_25to39*(av_fte_25to39)^{delta_pf}
	+ {phi_40to54_pf=1}*L_hc_40to54*(av_fte_40to54)^{delta_pf} 
	+ {phi_55p_pf=0.7} *L_hc_55p*(av_fte_55p)^{delta_pf})/
	(L_hc_lt25*(av_fte_lt25)^{delta_pf} + L_hc_25to39*(av_fte_25to39)^{delta_pf}
	+ L_hc_40to54*(av_fte_40to54)^{delta_pf} + L_hc_55p*(av_fte_55p)^{delta_pf})
	)";
	
local L_fte_del_wb "((
		(L_hc_m_ten0*(av_fte_m_ten0)^{delta_wb=1}+{phi_f_ten0_wb=1}*L_hc_f_ten0 *(av_fte_f_ten0)^{delta_wb}) 
	+ {phi_ten1_wb=1}    *
		(L_hc_m_ten1*(av_fte_m_ten1)^{delta_wb} + {phi_f_ten1_wb=0.9}*L_hc_f_ten1 *(av_fte_f_ten1)^{delta_wb}) 
	+ {phi_ten2_wb=1}  *
		(L_hc_m_ten2*(av_fte_m_ten2)^{delta_wb} + {phi_f_ten2_wb=0.9}*L_hc_f_ten2 *(av_fte_f_ten2)^{delta_wb}))*
	({phi_lt25_wb=0.5}*L_hc_lt25*(av_fte_lt25)^{delta_wb} + L_hc_25to39*(av_fte_25to39)^{delta_wb}
	+ {phi_40to54_wb=1}*L_hc_40to54*(av_fte_40to54)^{delta_wb} 
	+ {phi_55p_wb=0.7} *L_hc_55p*(av_fte_55p)^{delta_wb})/
	(L_hc_lt25*(av_fte_lt25)^{delta_wb} + L_hc_25to39*(av_fte_25to39)^{delta_wb}
	+ L_hc_40to54*(av_fte_40to54)^{delta_wb} + L_hc_55p*(av_fte_55p)^{delta_wb})
	)";
	

local vars_fte_del "`vars_fte'";

local L_fte_dell "Sum of head count*(average FTEs)^{delta} for all labour types";





	
	
	
	
********************************************************************************;
* Running regressions;

* using obs with hc of 5+ in consisten restricted sample;

use if Q_rest==1 using GWgap_pr_firm_v4, clear;

drop *ten* INpf_*;
gen con = 1;
label var con "Constant";


* merging in extra tenure breakdowns;

capture drop _m;
merge 1:1 pent year using GWgap_readin_v3b_firm_ten, keep(master match) nogen;


if "`samp'"=="on" {;
	replace pf_ind_gp = 10*floor(pf_ind_gp/10); * making fewer indy FE for sample run;
	replace Yyear_2003 =  Yyear_2003 + Yyear_2004;
	drop Yyear_2003 Yyear_2004;
	replace Yyear_2005 = Yyear_2005 + Yyear_2006 + Yyear_2007;
	drop Yyear_2006 Yyear_2007;
	replace Yyear_2008 = Yyear_2008 + Yyear_2009 + Yyear_2010;
	drop Yyear_2009 Yyear_2010;
};

rename pf_ind_gp ind_gp_pf;
xi i.ind_gp_pf, pre(IN);


unab extra_ctls: multiplant WPc_* INind* Yyear* ; 

di "`extra_ctls'";
compress;



capture erase "`outfile'`suff'.txt";
file open out using "`outfile'`suff'.txt", write;
file write out "The pf and wb regressions are translog form where an observation is
a pent in a year and the labour input is a linear combination of male and female labour
inputs interacted with tenure groups, and age categories not interacted." _n;

file write out "Regressions are weighted by total FTEs (other)." _n _n;
file close out;




compress;



foreach lab in fte_del {; * looping over way labour input is measured;

	file open out using "`outfile'`suff'.txt", write append;

	eststo clear;
	local outlist `""';

	if "`lab'"=="hc" local wgtvar L_hc_t;
	if "`lab'"=="fte" | "`lab'"=="ftpt" | "`lab'"=="fte_del" | "`lab'"=="hrs" | "`lab'"=="hrs_del"
		local wgtvar L_fte_t;


	local outlist `" `outlist' `lab' "';

		
	


	di as input _n _n "$S_DATE $S_TIME: Running reg for lab=`lab'";
						
				
	* which labour measure to use;
	
	di as input "L_pf = `L_`lab'_pf'" _n "`L_`lab'l'";
	file write out "Labour specification: `L_`lab'l'"; 
		
	preserve;
	
	
	
	* keeping only the subset of controls that vary;
	local extra_ctls2 "";
	foreach var in `extra_ctls' {;
		qui sum `var';
		if r(sd)>0 & r(sd)<. local extra_ctls2 "`extra_ctls2' `var'";
	};

	
	* listing industry FE in extra_ctls2;
	
	local indvars "";
	local nonindvars "";
	foreach var in `extra_ctls2' {;
		if strpos("`var'","INind")==1 local indvars "`indvars' `var'";
		if strpos("`var'","INind")!=1 local nonindvars "`nonindvars' `var'";
	};
	
	* omitting the first industry FE;
	
	local ind1: word 1 of `indvars';
	local indvars: list indvars - ind1;
	local extra_ctls2: list nonindvars | indvars;



	di "$S_DATE $S_TIME: Starting preliminary run of prodn fn only";

	* running the prodn fn only;
	nl (lngo = {lnL_pf=1}*ln(`L_`lab'_pf')	
			+ {lnLsq_pf=1}*ln(`L_`lab'_pf')*ln(`L_`lab'_pf')/100
			+ {lnL_lnK_pf=1}*ln(`L_`lab'_pf')*lnK/100
			+ {lnL_lnM_pf=1}*ln(`L_`lab'_pf')*lnM/100
			
			+ {lnK_pf=1}*lnK
			+ {lnKsq_pf=1}*lnKsq
			+ {lnK_lnM_pf=1}*lnK_lnM
			
			+ {lnM_pf=1}*lnM
			+ {lnMsq_pf=1}*lnMsq
			+ {xb_pf: `extra_ctls2' con})
		[aweight = `wgtvar'];
		
	* saving coeffs to use as starting values;
	matrix define coeffs_pf = e(b);
	
	di "$S_DATE $S_TIME: Starting preliminary run of wage bill eqn only";
	
	nl (lnWB = {lnL_wb=1}*ln(`L_`lab'_wb')	
			+ {lnLsq_wb=1}*ln(`L_`lab'_wb')*ln(`L_`lab'_wb')/100
			+ {lnL_lnK_wb=1}*ln(`L_`lab'_wb')*lnK/100
			+ {lnL_lnM_wb=1}*ln(`L_`lab'_wb')*lnM/100
			
			+ {lnK_wb=1}*lnK
			+ {lnKsq_wb=1}*lnKsq
			+ {lnK_lnM_wb=1}*lnK_lnM
			
			+ {lnM_wb=1}*lnM
			+ {lnMsq_wb=1}*lnMsq
			+ {xb_wb: `extra_ctls2' con})
		[aweight = `wgtvar'];
		
	* saving coeffs to use as starting values;
	matrix define coeffs_wb = e(b);
	
	matrix define coeffs = coeffs_pf, coeffs_wb;

	di "$S_DATE $S_TIME: Starting run";

	* running the main regression;
	nlsur (lngo = {lnL_pf}*ln(`L_`lab'_pf')	
			+ {lnLsq_pf}*ln(`L_`lab'_pf')*ln(`L_`lab'_pf')/100
			+ {lnL_lnK_pf}*ln(`L_`lab'_pf')*lnK/100
			+ {lnL_lnM_pf}*ln(`L_`lab'_pf')*lnM/100
			
			+ {lnK_pf}*lnK
			+ {lnKsq_pf}*lnKsq
			+ {lnK_lnM_pf}*lnK_lnM
			
			+ {lnM_pf}*lnM
			+ {lnMsq_pf}*lnMsq
			+ {xb_pf: `extra_ctls2' con})
		(lnWB = {lnL_wb}*ln(`L_`lab'_wb')	
			+ {lnLsq_wb}*ln(`L_`lab'_wb')*ln(`L_`lab'_wb')/100
			+ {lnL_lnK_wb}*ln(`L_`lab'_wb')*lnK/100
			+ {lnL_lnM_wb}*ln(`L_`lab'_wb')*lnM/100
			
			+ {lnK_wb}*lnK
			+ {lnKsq_wb}*lnKsq
			+ {lnK_lnM_wb}*lnK_lnM
			
			+ {lnM_wb}*lnM
			+ {lnMsq_wb=1}*lnMsq
			+ {xb_wb: `extra_ctls2' con})
		[aweight = `wgtvar']
		, cluster(pent) ifgnls initial(coeffs);
		
	di "Years included in main reg:";
	tab year if e(sample);

	
		
	di as input "Command just run: " e(cmdline);

	local command = e(cmdline);
	file write out _n "Command `lab':" _n "`command'" _n _n;

	/* use this line to confidentialise observation count;
	rndobs N;
	*/

	* Saving the difference between phi in pf and wb equations;
	
	local addsc "";
	local addst "";
	local addn "";

	foreach type in f_ten0 f_ten1 f_ten2 ten1 ten2 lt25 40to54 55p {;
	
		local phi_`type'_diff_c = 1 - _b[/phi_`type'_wb]/_b[/phi_`type'_pf];
		
		nlcom 1 - _b[/phi_`type'_wb]/_b[/phi_`type'_pf];
		matrix V = r(V);
		local phi_`type'_diff_se = V[1,1]^0.5;
		
		local addsc "`addsc' phi_`type'_diff_c `phi_`type'_diff_c' phi_`type'_diff_se `phi_`type'_diff_se'";
		local addst "`addst' phi_`type'_diff_c phi_`type'_diff_se";
		local addn `"`addn' "1 - phi_`type'_wb/phi_`type'_pf" "1 - phi_`type'_wb/phi_`type'_pf se" "';
	};
	

	eststo `lab', addscalars(`addsc');
	
			
	restore;
	file close out;


	local addstats "`addst'";
	local addfmts "%9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f";
	local addnames `" `addn' "';

	estout * using "`outfile'`suff'.txt", append
		c(b(star fmt(%9.3f)) se(par))
		style(tab)
		legend label collabels(, none)
		dropped ("dropped")
		eqlabels(`eqlab', merge) 
		varl(`labels')
		order(`order')
		indicate(`indicators')
		mlabels(`outlist')
		stats(r2 N converge `addstats', fmt(%9.3f %9.0gc %3.0f `addfmts') 
			labels("R-squared" Observations Converged `addnames'))
		varwidth(50) modelwidth(10) delimiter("") wrap
		prehead("`=char(13)'`=char(13)'
			`=char(13)'Run $S_TIME $S_DATE 
			`=char(13)'******************************************************"); 
};


